package com.usermanage.controller;

import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.*;
import com.usermanage.beanParam.CompanyBean;
import com.usermanage.util.JsonData;
import com.usermanage.util.StaticUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Optional;

/**
 *login  用户的登陆
 */
@Slf4j
@Api(description = "用户登陆注册接口")
@RestController
//@RequestMapping("/")
public class LoginController {
    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;

    @Resource
    private MessageService messageService;


//    @RequestMapping(value = {"/",""})
//    public ModelAndView salesLogin(){
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("url", StaticUtil.SKUIP);
//        mv.setViewName("/index/index");
//        return  mv;
//    }

    @RequestMapping(value={"/login","/",""},method= RequestMethod.GET)
    public ModelAndView loginPage() throws Exception{

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/login/login");
        return mv;
    }

    @RequestMapping(value="/logOut",method= RequestMethod.GET)
    public JsonData loginOut(boolean error) throws Exception{
        if (error){
            return JsonData.success("退出登陆成功");
        }else{
            return JsonData.fail("退出登陆失败");
        }
    }




    @RequestMapping(value="/index.page",method= RequestMethod.GET)
    public ModelAndView index() throws Exception{
        ModelAndView mv = new ModelAndView();
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        mv.addObject("userName",SpringSecurityUtils.getCurrentUser().getUsername());
        Optional<CompanyBean> optional = companyService.findById(Integer.valueOf(userDetail.getCompany_id()));
        mv.addObject("companyName",optional.get().getCompany_name());
        //初始化默认跳转到订单页面
        mv.addObject("url","/order/orderList.page");
        //查询当前登录用户未读消息条数
       int count = messageService.userUnReadCount(userDetail.getId().toString());
        mv.addObject("count",Integer.valueOf(count));
        mv.setViewName("/index");
        return mv;
    }


    /**
     * 用户登陆
     * @param request
     * @param username  用户邮箱（唯一）
     * @param password  用户密码
     * @return
     */
    @RequestMapping(value="/userLogin",method= RequestMethod.POST)
    public JsonData login(HttpServletRequest request, String username,String password,String url) throws Exception{
        log.info(username+"*****"+password);
        return userService.login(request,username,password,url,null);
    }


    /**
     * 第三方接口登陆获取用户信息
     * @param obj
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/loginInfo",method= RequestMethod.POST)
    public JsonData loginPort(String obj) throws Exception{
        log.info("**********"+obj);
        return userService.loginPort(obj);
    }

    /**
     * 第三方接口登陆获取用户信息
     * @param user_name
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/getUserId",method= RequestMethod.GET)
    public JsonData findUserIdForPort(String user_name,String companyId) throws Exception{
        log.info("**********"+user_name);
        Map map = userService.findUserIdForPort(user_name,companyId);
        if (StringUtils.isNotBlank(String.valueOf(map.get("id")))) {
            return JsonData.success(map);
        }else{
            return JsonData.fail();
        }
    }

    /**
     * 免登接口
     * @return
     */
//    @RequestMapping(value="/userLogin",method= RequestMethod.POST)
//    public ModelAndView login(HttpServletRequest request,String userId, String url) throws Exception{
//        ModelAndView mv = new ModelAndView();
//        mv.addObject();
//        return userService.oapiLogin(request,userId,url);
//    }

}
